import {ajaxGet} from "./wxajaxservice";
import {appconstant} from "./constant"
let token = ""
if( sessionStorage.getItem("TcslSlyunToken") ){
	token = sessionStorage.getItem("TcslSlyunToken")
}

 const generateAjaxGet = ( prefix ) => {
    console.log(prefix);
    return ( url ) => {
        //在此处写通用的get的方法
        return fetch( prefix+url ,{
			headers: {
			    'Tcsl-Slyun-Token': token
			}
		}).then( res=>res.json().then(
                    json => {
                        if( !res.ok ){
                            return Promise.reject(json)
                        }
                        return json
                    }
            )
        )
    }
}

const generateAjaxPost = (prefix) => {
    return ( url,data ) => {
        let headersobj = {
			'Accept': 'application/json',
	      	'Content-Type': 'application/json'
        }
        headersobj['Tcsl-Slyun-Token'] = token
        return fetch( prefix+url ,{
            headers: headersobj,
            method: 'post',
		    body: JSON.stringify( data ),
		 	mode:'cors'
		}).then( res=>res.json().then(
                    json => {
                        if( !res.ok ){
                            return Promise.reject(json)
                        }
                        return json
                    }
            )
        )
    }
}

export const syAjaxGet = generateAjaxGet(appconstant.servers[appconstant.servers.profile].host);
export const slyAjaxPost = generateAjaxPost(appconstant.servers[appconstant.servers.profile].host);
